package com.pb.wkflow.core.camunda.bpmn.instanceImpl;

import com.pb.wkflow.core.camunda.bpmn.instance.Participants;
import com.pb.wkflow.core.camunda.bpmn.instance.RuleLogicParam;
import com.pb.wkflow.core.camunda.bpmn.instance.UserParticipant;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.model.bpmn.impl.instance.BpmnModelElementInstanceImpl;
import org.camunda.bpm.model.xml.ModelBuilder;
import org.camunda.bpm.model.xml.impl.instance.ModelTypeInstanceContext;
import org.camunda.bpm.model.xml.instance.ModelElementInstance;
import org.camunda.bpm.model.xml.type.ModelElementTypeBuilder;
import org.camunda.bpm.model.xml.type.attribute.Attribute;
import org.camunda.bpm.model.xml.type.child.ChildElementCollection;
import org.camunda.bpm.model.xml.type.child.SequenceBuilder;

import java.util.Collection;

import static org.camunda.bpm.model.bpmn.impl.BpmnModelConstants.CAMUNDA_NS;

/**
 * 自定义用户节点参与者
 * @author 苗益辉
 * @date 2022/5/9 9:23
 */
@Slf4j
public class UserParticipantImpl extends BpmnModelElementInstanceImpl implements UserParticipant{

    /** 参与者类型 */
    protected static Attribute<String> participantTypeAttribute;
    /** 流程参与者 */
    protected static ChildElementCollection<Participants> participantsAttribute;
    /** 流程执行者 */
    protected static Attribute<String> activityAttribute;
    /** 相关数据 */
    protected static Attribute<String> aboutDataAttribute;
    /** 外部链接 */
    protected static Attribute<String> serviceUrlAttribute;
    /** 内部服务 */
    protected static Attribute<String> serviceBeanAttribute;
    /** 规则逻辑类型 外部/内部 */
    protected static Attribute<String> serviceTypeAttribute;
    /** 规则逻辑数据 */
    protected static ChildElementCollection<RuleLogicParam> ruleLogicParamAttributes;

    public UserParticipantImpl(ModelTypeInstanceContext instanceContext) {
        super(instanceContext);
    }

    public static void registerType(ModelBuilder modelBuilder) {
        log.info("注册自定义用户节点参与者 UserParticipant");
        ModelElementTypeBuilder typeBuilder = modelBuilder.defineType(UserParticipant.class, "userParticipant")
                .namespaceUri(CAMUNDA_NS)
                .instanceProvider(new ModelElementTypeBuilder.ModelTypeInstanceProvider<UserParticipant>() {
                    @Override
                    public UserParticipant newInstance(ModelTypeInstanceContext instanceContext) {
                        return new UserParticipantImpl(instanceContext);
                    }
                });

        SequenceBuilder sequenceBuilder = typeBuilder.sequence();

        participantTypeAttribute = typeBuilder.stringAttribute("participantType").namespace(CAMUNDA_NS).build();
        participantsAttribute = sequenceBuilder.elementCollection(Participants.class).build();
        activityAttribute = typeBuilder.stringAttribute("activity").namespace(CAMUNDA_NS).build();
        aboutDataAttribute = typeBuilder.stringAttribute("aboutData").namespace(CAMUNDA_NS).build();
        serviceUrlAttribute = typeBuilder.stringAttribute("serviceUrl").namespace(CAMUNDA_NS).build();
        serviceBeanAttribute = typeBuilder.stringAttribute("serviceBean").namespace(CAMUNDA_NS).build();
        serviceTypeAttribute = typeBuilder.stringAttribute("serviceType").namespace(CAMUNDA_NS).build();
        ruleLogicParamAttributes = sequenceBuilder.elementCollection(RuleLogicParam.class).build();
        typeBuilder.build();
    }

    @Override
    public String getParticipantType() {
        return participantTypeAttribute.getValue(this);
    }

    @Override
    public void setParticipantType(String participantType) {
        participantTypeAttribute.setValue(this, participantType);
    }

    @Override
    public Collection<Participants> getParticipants() {
        return participantsAttribute.get(this);
    }

    @Override
    public String getActivity() {
        return activityAttribute.getValue(this);
    }

    @Override
    public void setActivity(String activity) {
        activityAttribute.setValue(this, activity);
    }

    @Override
    public String getAboutData() {
        return aboutDataAttribute.getValue(this);
    }

    @Override
    public void setAboutData(String aboutData) {
        aboutDataAttribute.setValue(this, aboutData);
    }

    @Override
    public String getServiceUrl() {
        return serviceUrlAttribute.getValue(this);
    }

    @Override
    public void setServiceUrl(String serviceUrl) {
        serviceUrlAttribute.setValue(this,serviceUrl);
    }

    @Override
    public String getServiceBean() {
        return serviceBeanAttribute.getValue(this);
    }

    @Override
    public void setServiceBean(String serviceBean) {
        serviceBeanAttribute.setValue(this,serviceBean);
    }

    @Override
    public String getServiceType() {
        return serviceTypeAttribute.getValue(this);
    }

    @Override
    public void setServiceType(String serviceType) {
        serviceTypeAttribute.setValue(this,serviceType);
    }

    @Override
    public Collection<RuleLogicParam> getRuleLoginParam() {
        return ruleLogicParamAttributes.get(this);
    }

    @Override
    public void addChildElement(ModelElementInstance newChild) {
        getDomElement().appendChild(newChild.getDomElement());
    }
}
